package gui.ParkingLotWorker;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.ParkingLot;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import control.LocalOpertaionController;
import control.LoginController;
/**
 * this class use to Set Lot Status Screen
 * @author G8
 *
 */
public class SetLotStatusScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSet;
	public JComboBox comboBoxPLStatus;
	private LocalOpertaionController localOpControl;
	/**
	 *Set Lot Status Screen Constructor 
	 */
	public SetLotStatusScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		localOpControl = new LocalOpertaionController(null);
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Set Lot Status", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(InitializeLotScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnSet = new JButton("Set");
		btnSet.setBounds(193, 182, 105, 35);
		btnSet.addActionListener(this);
		add(btnSet);
		
		JLabel lblSetParkingLot = new JLabel("Set Parking Lot Status:");
		lblSetParkingLot.setBounds(88, 144, 152, 14);
		add(lblSetParkingLot);
		
		comboBoxPLStatus = new JComboBox();
		comboBoxPLStatus.setBounds(250, 141, 151, 20);
		comboBoxPLStatus.addItem("Not Full");
		comboBoxPLStatus.addItem("Full");
		add(comboBoxPLStatus);
		
		JLabel lblPleaseChooseThe = new JLabel("Please choose the status of this parking lot...");
		lblPleaseChooseThe.setBounds(88, 97, 313, 14);
		add(lblPleaseChooseThe);
		
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSet){
			SystemTypes lotToSet = new SystemTypes(VCP.currentLot,null);
			String str = comboBoxPLStatus.getSelectedItem().toString();
			SystemTypes setLotResult = localOpControl.FullParkingLot(lotToSet, str);
			
			if (setLotResult.getSysMode().equals(SystemMode.OK))
			{
				if (str.equals("Not Full"))
					JOptionPane.showMessageDialog(null, "Lot "+VCP.currentLot.getParkingLotNumber()+ " was successfully set as "+str+".", "Success",
	                    JOptionPane.INFORMATION_MESSAGE);
				else if (str.equals("Full"))
				{
					ParkingLot lot = (ParkingLot) setLotResult.getObj();
					JOptionPane.showMessageDialog(null, "Lot "+VCP.currentLot.getParkingLotNumber()+ " was successfully set as "+str+".\n Please reffer clients to lot number: " +lot.getParkingLotNumber(), "Success",
		                    JOptionPane.INFORMATION_MESSAGE);
				}
					
			}
			else{
				
				JOptionPane.showMessageDialog(null, setLotResult.getObj().toString(), "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}

}
